//
//  WBUserAccount.swift
//  WeiBo
//
//  Created by chenguanglei on 16/11/28.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import AFNetworking

class WBUserAccount: NSObject {

    var model : WBUserInfo?
    
    static let shareInstance = WBUserAccount()
    
    
    override init() {
        
        
        model = WBUserInfo.readInfo()
        
        super.init()
    }
    
    
    
    var isLogin : Bool {
    
        if isExpires {
            
            return false
        }else{
        
            return model?.access_token == nil ? false : true
            
        }
    }
    
    
    
    var isExpires : Bool {
    
        if model?.expires_date?.compare(Date()) == .orderedDescending {
            
            return false
            
        }
    
        return true
    }
    
    
    
    
    func loadToken (code:String,success:@escaping ()->(),fail:@escaping ()->()){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        
        let paremet = [
            
            "client_id":"180663250",
            "client_secret":"abeeab14aa0a86f4ed075c45a7a20539",
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":"http://www.weibo.com"
            
        ]
        
        
        let afn = AFHTTPSessionManager()
        
        afn.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        afn.post(urlString, parameters: paremet, progress: nil, success: { (task:URLSessionDataTask, json:Any?) in
            
            let dicJson = json as? [String:Any]
            
            print(dicJson)
            
            guard let dict = dicJson else{
                return
            }
            
            let userInfo = WBUserInfo(dic:dict)
            
//            print(userInfo.access_token)
//            
//            
//            //加载用户数据
//            guard let access_token = userInfo.access_token, let uid = userInfo.uid else{
//                
//                return
//            }
            
            self.userInfo(model: userInfo, success: success, fail: fail)
            
        }) { (task:URLSessionDataTask?, error:Error) in
            
            fail()
            
        }
        
    }
    
    
    
    func userInfo(model:WBUserInfo,success:@escaping ()->(),fail:@escaping ()->()){
        
        
        let urlString = "2/users/show.json"
        
        let paramete = [
            
            "access_token":model.access_token!,
            "uid":model.uid!
            
        ]
        
        WBHTTPClient.request(type: .get, urlString, parameters: paramete, success: { (json:Any) in
            
            let jsonModel = WBUserInfo(dic: json as! [String:Any])
            
            model.screen_name = jsonModel.screen_name
            model.profile_image_url = jsonModel.profile_image_url
            
            self.model = model
            
            model.saveInfo()
            
            success()
            
            
        }) { (error:Error) in
            
           fail()
            
        }
       
    }
   
    
        
    
    
    
}
